home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / lpd Sources / Dials.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  10.7 KB  |  417 lines  |  [TEXT/KAHL]

  1. /************************************************************************
  2.  *                                                                        *
  3.  *    Dials.c                                                                *
  4.  *                                                                        *
  5.  *  Line Printer Daemon using TCP/IP printer protocol                    *
  6.  *                                                                        *
  7.  *            -------------- The dialog routines --------------            *
  8.  *                                                                        *
  9.  *  Written by Casper Boon, August, 1992.                                *
  10.  *                                                                        *
  11.  *    © 1992 Casper Boon.                                                    *
  12.  *                                                                        *
  13.  ************************************************************************/
  14.  
  15. #include "LPD.H"
  16. #include "CvtAddr.h"
  17. #include "lpdProtos.H"
  18.  
  19.  
  20. /************************************************************************
  21.  *                                                                        *
  22.  *                                                                        *
  23.  ************************************************************************/
  24. integer doAbout()
  25. {
  26.     DialogRecord    DialStorg;
  27.     DialogPtr        aboutDial;
  28.     WindowPtr        aPort;
  29.     integer            ItemHit;
  30.     Rect            aRect, bRect;
  31.  
  32.     GetPort(&aPort);
  33.     SetRect(&aRect, 10, -15, 15, -10);
  34.     SetPort( aboutDial = GetNewDialog(AboutDLOG,
  35.                                         (Ptr)&DialStorg, (WindowPtr) -1) );
  36.     bRect = aboutDial->portRect;
  37.     bRect.top -= 20;
  38.     LocalToGlobal(&topLeft(bRect));
  39.     LocalToGlobal(&botRight(bRect));
  40.     ZoomRect(&aRect, &bRect, TRUE);
  41.     ShowWindow(aboutDial);
  42.  
  43.     InstallUserItem(aboutDial, abtDefFrm, (ProcPtr)DefaultFrame);
  44.  
  45.     ItemHit = 0;
  46.     while (ItemHit != okBtn)
  47.         ModalDialog(NIL, &ItemHit);
  48.  
  49.     SetPort(aPort);
  50.     CloseDialog(aboutDial);
  51.     ZoomRect(&aRect, &bRect, FALSE);
  52.  
  53.     return ItemHit;
  54. }
  55.  
  56. /************************************************************************
  57.  *                                                                        *
  58.  *                                                                        *
  59.  ************************************************************************/
  60. Boolean ValidHost(LongWord hostID)
  61. {
  62.     integer        nHosts, i;
  63.     LongWord    *hst;
  64.     Boolean        res = FALSE;
  65.     nHosts = GetHandleSize(hosts) / sizeof(LongWord);
  66.  
  67.     if (nHosts)
  68.         {
  69.         HLock(hosts);
  70.         hst = (LongWord*)(*hosts);
  71.         for (i = 0; i < nHosts; i++)
  72.             {
  73.             if (hostID == hst[i])
  74.                 {
  75.                 res = TRUE;
  76.                 break;
  77.                 }
  78.             }
  79.         HUnlock(hosts);
  80.         }
  81.     return res;
  82. }
  83.  
  84. /************************************************************************
  85.  *                                                                        *
  86.  *                                                                        *
  87.  ************************************************************************/
  88. Boolean ValidWindow(DialogPtr dial);
  89. Boolean ValidWindow(DialogPtr dial)
  90. {
  91.     WindowPtr    window;
  92.  
  93.     window = FrontWindow();        /* get the current front window */
  94.  
  95.     while (window != NIL)
  96.         {
  97.         if (window == dial)
  98.             return TRUE;
  99.         window = (WindowPtr)((WindowPeek)window)->nextWindow;
  100.         }
  101.     return FALSE;
  102. }
  103.  
  104. /************************************************************************
  105.  *                                                                        *
  106.  *                                                                        *
  107.  ************************************************************************/
  108. /* return FALSE if you want the main event loop to process this event */
  109. integer HandleDEvent(EventRecord *myEvent)
  110. {
  111.     DialogPtr    whichDial = 0;
  112.     integer        itemHit = 0;
  113.     GrafPtr        aPort;
  114.  
  115.  
  116.     if (myEvent->what == updateEvt)
  117.         {
  118.         whichDial = (DialogPtr)myEvent->message;
  119.         if (!ValidWindow(whichDial)) return TRUE;
  120.         GetPort(&aPort);
  121.         SetPort(whichDial);
  122.         BeginUpdate(whichDial);
  123.         DrawDialog(whichDial);
  124.         EndUpdate(whichDial);
  125.         SetPort(aPort);
  126.         return FALSE;
  127.         }
  128.  
  129.     DialogSelect(myEvent, &whichDial, &itemHit);
  130.  
  131.     if (!ValidWindow(whichDial)) return TRUE;
  132.  
  133.     if (((WindowPeek)whichDial)->windowKind < 0)
  134.         return TRUE;    /* a system window, we should never have got here */
  135.  
  136.     return TRUE;
  137. }
  138.  
  139.  
  140. /************************************************************************
  141.  *                                                                        *
  142.  * Banner processing.  Handles putting up the dialogues for processing,    *
  143.  * changing the messages and tearing the banners down again.            *
  144.  *                                                                        *
  145.  ************************************************************************/
  146. DialogPtr         lpd_ban = NIL;
  147. DialogRecord    lpd_rec;
  148. integer            lpd_lst = 0;
  149. DialogPtr         prt_ban = NIL;
  150. DialogRecord    prt_rec;
  151. integer            prt_scl = 0;
  152. integer            prt_clk = 0;
  153.  
  154. pascal void Clock(DialogPtr dial, integer item);
  155. pascal void Scaler(DialogPtr dial, integer item);
  156.  
  157. /************************************************************************
  158.  *                                                                        *
  159.  *                                                                        *
  160.  ************************************************************************/
  161. void Banners_Down()
  162. {
  163.     if (lpd_ban) CloseDialog(lpd_ban);
  164.     lpd_ban = NIL;
  165.     if (prt_ban) CloseDialog(prt_ban);
  166.     prt_ban = NIL;
  167. }
  168.  
  169. /************************************************************************
  170.  *                                                                        *
  171.  *                                                                        *
  172.  ************************************************************************/
  173. void Banners_Back()
  174. {
  175.     if (lpd_lst) LPD_Banner_Up();
  176.     if (prt_scl) PRT_Banner_Up("\p");
  177.     if (prt_clk) PSTAT_Banner_Up("\p");
  178. }
  179.  
  180. /************************************************************************
  181.  *                                                                        *
  182.  *                                                                        *
  183.  ************************************************************************/
  184. pascal void Scaler(DialogPtr dial, integer item)
  185. {
  186.     Handle        hand;
  187.     integer        type;
  188.     Rect        rect;
  189.     LongInt        pct = GetWRefCon(dial);
  190.  
  191.     SetPort(dial);
  192.     GetDItem(dial, item, &type, &hand, &rect);
  193.     FrameRect(&rect);
  194.     InsetRect(&rect, 1, 1);
  195.     rect.right = ( ( (rect.right - rect.left) * pct ) / 100 ) + rect.left;
  196.     FillRect(&rect, gray);
  197. }
  198.  
  199. /************************************************************************
  200.  *                                                                        *
  201.  *                                                                        *
  202.  ************************************************************************/
  203. pascal void Clock(DialogPtr dial, integer item)
  204. {
  205.     Handle        hand;
  206.     integer        type;
  207.     Rect        rect;
  208.     LongInt        tick = Ticks % 360;
  209.     LongInt        pat = GetWRefCon(dial);
  210.  
  211.     SetPort(dial);
  212.     GetDItem(dial, item, &type, &hand, &rect);
  213.     PenSize(4, 4);
  214.     FrameOval(&rect);
  215.     InsetRect(&rect, 6, 6);
  216.     EraseArc(&rect, tick,     90);
  217.      FillArc(&rect, tick+ 90, 90, black);
  218.     EraseArc(&rect, tick+180, 90);
  219.      FillArc(&rect, tick+270, 90, black);
  220.  
  221.     PenNormal();
  222. }
  223.  
  224. /************************************************************************
  225.  *                                                                        *
  226.  *                                                                        *
  227.  ************************************************************************/
  228. void LPD_Banner_Up()
  229. {
  230.     if (lpd_ban) return;
  231.     lpd_lst = rcvFlDLOG;
  232.  
  233.     if (inBackground) return;
  234.  
  235.     lpd_ban = GetNewDialog(rcvFlDLOG, &lpd_rec, (WindowPtr) -1L);
  236.  
  237.     if (!lpd_ban) return;
  238.  
  239.     SetWRefCon(lpd_ban, 0);
  240.     InstallUserItem(lpd_ban, barUsr, (ProcPtr)Scaler);
  241.     DrawDialog(lpd_ban);
  242. }
  243.  
  244. /************************************************************************
  245.  *                                                                        *
  246.  *                                                                        *
  247.  ************************************************************************/
  248. void LPD_Banner_Down()
  249. {
  250.     if (lpd_ban)
  251.         CloseDialog(lpd_ban);
  252.     lpd_ban = NIL;
  253.     lpd_lst = 0;
  254. }
  255.  
  256. /************************************************************************
  257.  *                                                                        *
  258.  *                                                                        *
  259.  ************************************************************************/
  260. void LPD_Banner_PCent(LongInt done, LongInt want)
  261. {
  262.     LongInt    percent;
  263.     if (!lpd_ban) return;
  264.     if (want > 0) percent = (done * 100)/want;
  265.     else          percent = 100;
  266.     SetWRefCon(lpd_ban, percent);
  267.     Scaler(lpd_ban, barUsr);
  268. }
  269.  
  270. /************************************************************************
  271.  *                                                                        *
  272.  *                                                                        *
  273.  ************************************************************************/
  274. void PRT_Banner_Up(StringPtr status)
  275. {
  276.     if (prt_ban) return;
  277.     prt_scl = prtFlDLOG;
  278.  
  279.     if (inBackground) return;
  280.  
  281.     prt_ban = GetNewDialog(prtFlDLOG, &prt_rec, (WindowPtr) -1L);
  282.  
  283.     if (!prt_ban) return;
  284.  
  285.     SetDialText(prt_ban, stusTxt, status);
  286.     SetWRefCon(prt_ban, 0);
  287.     InstallUserItem(prt_ban, barUsr, (ProcPtr)Scaler);
  288.     DrawDialog(prt_ban);
  289. }
  290.  
  291. /************************************************************************
  292.  *                                                                        *
  293.  *                                                                        *
  294.  ************************************************************************/
  295. void PRT_Banner_Down()
  296. {
  297.     if (prt_ban)
  298.         CloseDialog(prt_ban);
  299.     prt_ban = NIL;
  300.     prt_scl = 0;
  301.     prt_clk = 0;
  302. }
  303.  
  304. /************************************************************************
  305.  *                                                                        *
  306.  *                                                                        *
  307.  ************************************************************************/
  308. Boolean NewMessage(integer which, StringPtr message);
  309. Boolean NewMessage(integer which, StringPtr message)
  310. {
  311.     Str255    buffer;
  312.     Byte    *b, *m;
  313.     Byte    c;
  314.     if (!message) return FALSE;
  315.     GetDialText(prt_ban, which, buffer);
  316.     b = buffer; m = message;
  317.     c = *b + 1;
  318.     while (c--)
  319.         if (*b++ != *m++) return TRUE;
  320.     return FALSE;
  321. }
  322.  
  323. /************************************************************************
  324.  *                                                                        *
  325.  *                                                                        *
  326.  ************************************************************************/
  327. void PRT_Banner_PCent(LongInt done, LongInt want, StringPtr message)
  328. {
  329.     LongInt    percent;
  330.     if (!prt_ban) return;
  331.     if (want > 0) percent = (done * 100)/ want;
  332.     else          percent = 100;
  333.     SetWRefCon(prt_ban, percent);
  334.     if (NewMessage(stusTxt, message))
  335.         SetDialText(prt_ban, stusTxt, message);
  336.     Scaler(prt_ban, spinUsr);
  337. }
  338.  
  339. /************************************************************************
  340.  *                                                                        *
  341.  *                                                                        *
  342.  ************************************************************************/
  343. void PSTAT_Banner_Up(StringPtr status)
  344. {
  345.     if (prt_ban) return;
  346.     prt_clk = pStatDLOG;
  347.  
  348.     if (inBackground) return;
  349.     prt_ban = GetNewDialog(pStatDLOG, &prt_rec, (WindowPtr) -1L);
  350.  
  351.     if (!prt_ban) return;
  352.  
  353.     SetDialText(prt_ban, stusTxt, status);
  354.     SetWRefCon(prt_ban, 0);
  355.     InstallUserItem(prt_ban, spinUsr, (ProcPtr)Clock);
  356.     DrawDialog(prt_ban);
  357. }
  358.  
  359. /************************************************************************
  360.  *                                                                        *
  361.  *                                                                        *
  362.  ************************************************************************/
  363. void PSTAT_Banner_Step(StringPtr message)
  364. {
  365.     if (!prt_ban) return;
  366.     if (NewMessage(stusTxt, message))
  367.         SetDialText(prt_ban, stusTxt, message);
  368.     Clock(prt_ban, spinUsr);
  369. }
  370.  
  371.  
  372. /************************************************************************
  373.  *                                                                        *
  374.  *                                                                        *
  375.  ************************************************************************/
  376. void POPEN_Banner_Up(StringPtr name, StringPtr status)
  377. {
  378.     Str255    buf;
  379.  
  380.     if (prt_ban) return;
  381.     prt_clk = pStatDLOG;
  382.  
  383.     if (inBackground) return;
  384.     prt_ban = GetNewDialog(pStatDLOG, &prt_rec, (WindowPtr) -1L);
  385.  
  386.     if (!prt_ban) return;
  387.  
  388.     psprintf(buf, "Looking for Printer %p", name);
  389.     SetDialText(prt_ban, whyText, buf);
  390.     SetDialText(prt_ban, stusTxt, status);
  391.     SetWRefCon(prt_ban, 0);
  392.     InstallUserItem(prt_ban, spinUsr, (ProcPtr)Clock);
  393.     DrawDialog(prt_ban);
  394. }
  395.  
  396.  
  397. /************************************************************************
  398.  *                                                                        *
  399.  *                                                                        *
  400.  ************************************************************************/
  401. void CONFIG_Banner_Up()
  402. {
  403.     if (prt_ban) return;
  404.     prt_clk = pStatDLOG;
  405.  
  406.     if (inBackground) return;
  407.     prt_ban = GetNewDialog(pStatDLOG, &prt_rec, (WindowPtr) -1L);
  408.  
  409.     if (!prt_ban) return;
  410.  
  411.     SetDialText(prt_ban, whyText, "\pReading Config File");
  412.     SetDialText(prt_ban, stusTxt, "\p");
  413.     SetWRefCon(prt_ban, 0);
  414.     InstallUserItem(prt_ban, spinUsr, (ProcPtr)Clock);
  415.     DrawDialog(prt_ban);
  416. }
  417.